﻿<?php

class ShowPlanetPage extends AbstractPage
{	
	public static $requireModule = 0;

	function __construct() 
	{
		parent::__construct();
	}

	function show()
	{
	global $USER, $PLANET, $CONF;
		
		$type = 200;
			$power = 1.1;
			if($PLANET['planet_type'] == 3)
			{
				$type = 15000;
				$power = 1.15;
			}

		$this->tplObj->loadscript('planet.js');
		$this->tplObj->assign_vars(array(
			'planet_type' 		=> $PLANET['planet_type'],	
			'power' 			=> $power,	
			'type' 				=> $type,	
			'purchased_fields'	=> $PLANET['purchased_fields'],
			'Field' 			=> CalculateMaxPlanetFields($PLANET),
			'galaxy'			=> $PLANET['galaxy'],	
			'system'			=> $PLANET['system'],	
			'planet'			=> $PLANET['planet'],	
			));
		$this->display("page.planet.tpl");
	}
   
    function field()
	{
		global $USER, $PLANET, $CONF;
			
			$filds	= HTTP::_GP('filds', 0);
			
			$type = 200;
			$power = 1.1;
			if($PLANET['planet_type'] == 3)
			{
				$type = 15000;
				$power = 1.15;
			}
			
			
			$purchased_fields	= $PLANET['purchased_fields'];
			
			$cost = 0;
			
			for($i = 0; $i < $filds; $i++ )
			{
				$cost_i = round($type * pow($power,($purchased_fields + $i)));
				$cost += $cost_i;
			}
						
			if($USER['darkmatter']>=$cost) 
			{ 
				$USER['darkmatter']-=$cost;
				$GLOBALS['DATABASE']->multi_query("UPDATE ".USERS." SET `darkmatter` = `darkmatter` - ".$cost." WHERE id=".$USER['id'].";UPDATE ".PLANETS." SET `field_max` = `field_max` + ".$filds.", `purchased_fields` = `purchased_fields` + ".$filds." WHERE `id` = '".$PLANET['id']."';");
				$this->printMessage("Поля добавлены",true,  array("game.php?page=planet", 1));
			}
			else					
			{
				$this->printMessage("У вас не хватает ТМ",true,  array("game.php?page=planet", 1));
			}
	}

		
   
    function coord()
	{
		global $USER, $PLANET, $CONF;
    		$galaxy	= HTTP::_GP('galaxy', 0);
     		$system	= HTTP::_GP('system', 0);
     		$planet	= HTTP::_GP('planets', 0);
     		
			$cost 	= 250 * abs($PLANET['system'] - $system) + 15000 * abs($PLANET['galaxy'] - $galaxy) + 50;
			
    if ($CONF['max_galaxy'] < $galaxy || $galaxy == 0 || $galaxy < 0)
	{
         $this->printMessage("Ошибка координат",true,  array("game.php?page=planet", 1));
    }
	elseif($CONF['max_system'] < $system || $system == 0 || $system < 0)
	{
         $this->printMessage("Ошибка координат",true,  array("game.php?page=planet", 1));
    }
	elseif($CONF['max_planets'] < $planet || $planet == 0 || $planet < 0)
	{
         $this->printMessage("Ошибка координат",true,  array("game.php?page=planet", 1));
    }
	else
	{
		
		
		if($USER['darkmatter']>=$cost) 
		{
			$Controllo	= $GLOBALS['DATABASE']->countquery("SELECT COUNT(*) FROM ".PLANETS." WHERE `universe` = '".$USER['universe']."' AND `galaxy` = '".$galaxy."' AND `system` = '".$system."' AND `planet` = '".$planet."';");
			//Flottenbewegung?
	   		$qry		= $GLOBALS['DATABASE']->query("SELECT * FROM ".FLEETS." WHERE `fleet_start_id` = '".$PLANET['id']."' OR `fleet_end_id` = '".$PLANET['id']."'".($PLANET['id_luna'] != 0 ? " OR `fleet_start_id` = '".$PLANET['id_luna']."' OR `fleet_start_id` = '".$PLANET['id_luna']."'":"").";");
	
			$ATTACK = $GLOBALS['DATABASE']->fetch_array($qry);
			if($ATTACK['fleet_owner'] == $USER['id'] OR $ATTACK['fleet_target_owner'] == $USER['id'])
			{
				$this->printMessage("Флот в полёте.", true,  array("game.php?page=planet", 2));
				return;
			}
			
		
			if ($PLANET['planet_type'] == 3)
			{
				$this->printMessage("Действие запрешено вы находитенсь на  ЛУНЕ.", true,  array("game.php?page=planet", 2));
				return;
			}
		
		
			if($Controllo == 0)
			{	
				$USER['darkmatter']-= $cost;
				$GLOBALS['DATABASE']->multi_query(
				"UPDATE ".PLANETS." SET 
				`galaxy` = '".$galaxy."',
				`system` = '".$system."',
				`planet` = '".$planet."'
				 WHERE `id` = '".$PLANET['id_luna']."';
				UPDATE ".PLANETS." SET 
				`galaxy` = '".$galaxy."',
				`system` = '".$system."',
				`planet` = '".$planet."' WHERE
				`id` = '".$PLANET['id']."';
				UPDATE ".CONFIG." SET `LastSettedGalaxyPos` ='".$galaxy."' , `LastSettedSystemPos` = '".$system."' , `LastSettedPlanetPos` = '".$planet."' WHERE `uni` = '".$USER['universe']."';");
				
				if($USER['id_planet'] == $PLANET['id'])
				{
					$GLOBALS['DATABASE']->query("UPDATE ".USERS." SET`galaxy` = '".$galaxy."' , `system` = '".$system."', `planet` = '".$planet."' WHERE `id` = '".$USER['id']."';");
				}
				 $this->printMessage("Планета телепортированна.", true,  array("game.php?page=planet", 1));
			}
			else
			{
				$this->printMessage("Координаты заняты", true,  array("game.php?page=planet", 2));
			}
		} 
		else 
		{
			$this->printMessage("У вас не хватает Темной материи", true,  array("game.php?page=planet", 2));
		}
	}
  }
}
?>

